#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/28 14:50
# @Author : 闫峰
# @File    : redis_utils.py
import redis

from utils.readFileUtils.yaml_control import GetYamlData
from utils.settings import ensure_path_sep


class RedisClient:
    def __init__(self,database_key):
        # database_key = 'walmart.yaml:test_jdw_redis:8'
        yaml_path, database_key, db_name = database_key.split(":")
        yaml_path = ensure_path_sep(f"\\conf\\{yaml_path}")

        self.config = GetYamlData(yaml_path).get_yaml_data()[database_key]
        self.redis = redis.Redis(host=self.config['host'], port=self.config['port'], db=db_name, password=self.config['password'])

    def set(self, key, value):
        self.redis.set(key, value)

    def get(self, key):
        return self.redis.get(key).decode('utf-8')

    def delete(self, key):
        self.redis.delete(key)

    def get_keys(self,a):
        key_names = self.redis.keys(a,encodings='utf-8')
        self.redis.hset()
        return [name.decode('utf-8') for name in key_names]


if __name__ == '__main__':
    database_key = 'walmart.yaml:test_jdw_redis:8'
    r = RedisClient(database_key)

    setup_sql = "delete:walmart:feed:sku:11623"
    funcname = setup_sql.split(":",maxsplit=1)[0]
    key = setup_sql.split(":",maxsplit=1)[1]
    func = getattr(r, funcname)
    func(key)

    print(r.get_keys('walmart:feed:sku*'))
    print(r.get("ATTRIBUTE:TRANSLATION:ages_18_and_over_x_rated"))
    r.delete("walmart:feed:sku:11465")
